package com.vint;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Keylistener extends MouseAdapter implements KeyListener,ActionListener{

	HeroPlane hp = new HeroPlane();
	ArrayList<Boom> herobooms = new ArrayList<>();
	
	private boolean L;
	private boolean R;
	private boolean U;
	private boolean D;
	JLabel label1;
	JLabel label2;
	JLabel label3;
	JLabel label4;
	JLabel label5;
	JLabel label6;
	
	JPanel panel;
	
	Thread th;
	Thread th1;
	Thread th2;
	Thread th3;
	
	JFrame f;
	
	

	@Override
	public void keyPressed(KeyEvent e) {

		switch (e.getKeyCode()) {
		case KeyEvent.VK_LEFT:
			L=true;
			break;
		case KeyEvent.VK_RIGHT:
			R=true;
			break;
		case KeyEvent.VK_UP:
			U=true;
			break;
		case KeyEvent.VK_DOWN:
			D=true;
			break;
		case KeyEvent.VK_SPACE:
			if(hp.TP>=0){
				Boom boom = new Boom(0,hp.locx+55,hp.locy);
				herobooms.add(boom);
				Boom boom2 = new Boom(0, hp.locx+15,hp.locy);
				herobooms.add(boom2);
				hp.TP = hp.TP - 10;
			}
			break;
			default:
				break;
		}
		
			if(D){
				hp.locy = hp.locy+hp.speedy;
				if(hp.locy+hp.image.getHeight(null)>960){
					hp.locy = 960-hp.image.getHeight(null);
				}
			}else if(U){
				hp.locy = hp.locy-hp.speedy;
				if(hp.locy<40){
					hp.locy = 40;
				}
			}
			if(L){
				hp.locx = hp.locx-hp.speedx;
				if(hp.locx<2){
					hp.locx = 2;
				}
			}else if(R){
				hp.locx = hp.locx+hp.speedx;
				if(hp.locx+hp.image.getWidth(null)>598){
					hp.locx = 598-hp.image.getWidth(null);
					
				}
			}
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		switch (e.getKeyCode()) {
		case KeyEvent.VK_LEFT:
			L=false;
			break;
		case KeyEvent.VK_RIGHT:
			R=false;
			break;
		case KeyEvent.VK_UP:
			U=false;
			break;
		case KeyEvent.VK_DOWN:
			D=false;
			break;
		}
		

	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	 @Override
	 public void mouseClicked(MouseEvent e) {
		 int x = e.getX();
		 int y = e.getY();
		 if(in(x,y,label1)){
			 hp.type = 1;
		 }else if(in(x,y,label2)){
			 hp.type = 2;
		 }else if(in(x,y,label3)){
			 hp.type = 3;
		 }else if(in(x,y,label4)){
			 hp.type = 4;
		 }else if(in(x,y,label5)){
			 hp.type = 5;
		 }else if(in(x,y,label6)){
			 hp.type = 6;
		 }else{
			 
		 }
		 hp.image = hp.chooseimage(hp.type);
	 }
	 public boolean in(int x,int y,JLabel obj){
		 boolean result = false;
		 if(x<obj.getX()+obj.getWidth()&&x>obj.getX()&&y>obj.getY()&&y<obj.getY()+obj.getHeight()){
			 result = true;
		 }
		 
		 return result;
	 }
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		f.setVisible(false);
		panel.requestFocus();
		th.start();
		th1.start();
		th2.start();
		th3.start();
		System.out.println(hp.type);
	}

	
}
